//app.js
import * as echarts from './ec-canvas/echarts';
// var wxCharts = require('./utils/charts.js');
App({
  echarts,
  // wxCharts,
  reqUrl:"https://www.normalsys.cn/tonometer",
  onLaunch: function () {
    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

    // 登录
    this.login();
    // 获取用户信息
  },
  login() {
    var that = this
    //调用登录接口
    wx.login({
      success: function (res) {
        console.log(res);
        if (res.code) {
          // 发起网络请求
          wx.request({
            method: 'POST',
            url: getApp().reqUrl + '/login/wechat',
            header: { 'content-type': 'application/json' },
            data: {
              code: res.code,
            },
            success: function (res) {
              console.log(res);
              if(res.data.code == 200){
                that.globalData.loginInfo = res.data.msg
                that.getUser();
                console.log('登录')
              }else{
                wx.showModal({
                  title: '登录失败，请重试',
                })
              }

              // 获得sessionID
              // typeof cb == "function" && cb(res.data.messages, that.globalData.sessionID);
            },
            fail: function (res) {
              console.log(res);
            }
          })

        } else {
          console.log('获取用户登录态失败！' + res.errMsg)
        }

      }
    })
  },
  // 获取用户信息
  getUser(cb){
    var that = this;
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              console.log(res, 'user')
              // 可以将 res 发送给后台解码出 unionId
              that.globalData.isAuth = true;
              var data = {
                encryptedData: res.encryptedData,
                iv: res.iv,
              }
              console.log(data,'授权数据传')
              that.request({
                url:'/login/auth',
                data,
                success(res){
                  console.log(res,'授权----')
                  if(res.data.code==200){
                    var msg = res.data.msg;
                    that.globalData.authInfo = msg
                    if(!msg.isBind){
                      wx.navigateTo({
                        url: '/pages/login/bind/index',
                      })
                    }
                  }
                }
              })
          

              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        } else {
          console.log('未授权')
          that.globalData.isAuth = false
        }
      },
    })
  },
  request(obj){
    var that = this;
    obj.data = obj.data || {};
    obj.url = that.reqUrl + obj.url;
    obj.method = obj.method || 'POST';
    obj.header = {
      'content-type': 'application/json',
       token: that.globalData.loginInfo.token,
    };
    obj.fail = function () {
      wx.showToast({
        title: '请求失败，请重试',
        icon: 'none'
      })
    }
    obj.complete = function (res) {
      if (res.data.code != 200) {
        wx.showToast({
          title: res.data.msg||'请求失败',
          icon: 'none'
        })
      }
    }
    wx.request(obj);
  },
  globalData: {
    userInfo: null,
    loginInfo:null,
    isAuth:false,
    authInfo:{
      isBind:false,
    }
  }
})